﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
namespace FTchina
{
    /// <summary>
    ///DataTimeFormat 的摘要说明
    /// </summary>
    public class DataTimeFormat
    {
        public DataTimeFormat()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 将输入的时间进行格式化整理
        /// </summary>
        /// <param name="time">输入的时间</param>
        /// <param name="type">
        /// 1：yyyy年MM月dd日
        /// 2：MM月dd日
        /// 3：MM月dd日H时
        /// 4：yyyy年MM月dd日H时
        /// </param>
        /// <returns></returns>
        public static string FormatTime(string time, string type)
        {
            if (type == "0")
            {
                return FormatTime(Convert.ToDateTime(time));
            }
            else
            {
                DateTime dt = Convert.ToDateTime(time);
                string DF = string.Empty;
                switch (type)
                {
                    case "1": { DF = "yyyy年MM月dd日"; break; }
                    case "2": { DF = "MM月dd日"; break; }
                    case "3": { DF = "MM月dd日H时"; break; }
                    case "4": { DF = "yyyy年MM月dd日H时"; break; }
                    default: { DF = "d"; break; }
                }
                return dt.ToString(DF, System.Globalization.DateTimeFormatInfo.InvariantInfo);
            }
        }


        public static string FormatTime(DateTime theTime)
        {
            return FormatTime(theTime, "");
        }

        public static string FormatTime(DateTime theTime, string dateFormat)
        {
            string str;
            if (dateFormat != "")
            {
                return theTime.ToString(dateFormat, DateTimeFormatInfo.InvariantInfo);
            }
            DateTime time = DateTime.Today.AddDays(-1.0);
            DateTime time2 = new DateTime(DateTime.Today.Year, 1, 1);
            if (theTime < time2)
            {
                str = theTime.ToString("yyyy-MM-dd");
            }
            else
            {
                if (theTime < time)
                {
                    return theTime.ToString("MM月dd日");
                }
                if (theTime < DateTime.Today)
                {
                    str = "昨 ";
                    str = str + theTime.ToString("HH:mm");
                }
                else
                {
                    str = "今 ";
                    str = str + theTime.ToString("HH:mm");
                }
            }
            return str;
        }

    }
}